var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var communityUrl = require("url/community");
var communityMessageUrl = communityUrl.url.message.create;
var qqHttp = require("helper/http");
var httpSilence = require("helper/httpSilence");
var imgTool = require("helper/picture");
var helperOpen = require("helper/open");
var userLoginHelper = require("helper/userLogin");

// UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");
var layoutForm=ui("layoutForm");
var root = ui("$");
// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
labelNext.visible = true;
headerLeft.on("touch", function() {
	do_App.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "发邻里动态",
	"labelNext" : "发布"
})
header.refreshData();
var tagList;
//页面装载完成后，开始初始化工作
do_Page.on("loaded", function(){
	//load就拿标签获数据，避免每次点标签都去拿数据
	httpSilence.post(communityUrl.url.message.tagList, {}, function(data) {
		tagList=data.tagList;
	})
});

/** **************处理-图片显示UI-end**************** */
var aLayoutImg = ui("aLayoutImg");
var imgGridView =  ui("imgGridView");
var imgMax = 9;  //最多显示图片
var imgsData = []; //保存图片数组
/*新增图片*/
aLayoutImg.on("touch", function(data, e) {
	if (imgMax==0) {
		return;
	}
	imgTool.imgMulti(imgMax, selectImgBack);// 选择多张图片
});
function selectImgBack(imgList) {
	imgMax=imgMax-imgList.length;
	imgsData = imgsData.concat(imgList.map(function(path) {
		return {
			"imgPath" : path
		}
	}));
	imgGridView.items = imgsData;
}
/*删除图片*/
imgGridView.on("touch", function(data, e) {
	imgMax=imgMax+1;
	imgsData.splice(data,1);
	imgGridView.items = imgsData;
});
/** **************处理-图片-end**************** */


var layout_main = ui('do_LinearLayout_main');
var addboxContent = layout_main.add("boxContent",
		"source://view/reuse/form/mutiTextarea.ui", 0, 0);
var boxContent = ui("boxContent");
hashdata.addData({
	"textAreaHint" : '话题内容'
});
boxContent.bindData(hashdata);
boxContent.refreshData();
var textBoxContent = ui(addboxContent + ".textArea");

var addPicker = aLayoutRoot.add("picker",
		"source://view/reuse/form/oneCategory.ui", 0, 0);
//选择标签
var addTagName = layout_main.add("tagName","source://view/reuse/form/mutiLabel.ui", 0, 0);
var tagName = ui("tagName");
tagName.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "选择标签",
	"labelText" : '',
	"labelTipsVisible" : false,
	"labelTips" : ""
});
tagName.refreshData();
var labelTagName = ui(addTagName + ".labelText");
ui(addTagName + ".labelAout").on("touch", "", 300, function() {
	do_Page.hideKeyboard();
	var params = {}
	var ptitle = {
		"title" : "选择标签",
		"id" : "tagId"
	}
	var pcount = [ ptitle, tagList];
	do_Page.fire("oneCategoryList", pcount);
	do_Page.fire("oneCategory", "true");
});
var tagId;
do_Page.on("refreshOneCategory", function(data) {
	if("tagId" == data[1]){
		labelTagName.text = data[0].onename;
		tagId = data[0].oneid;
	}
});

ui("do_ScrollView_1").redraw();
headerRight.on("touch", "", 5000, function() {
	create();
})

function create() {
	do_Page.hideKeyboard();
	var length = imgsData.length;
	if (textBoxContent.text.length<=0 && length == 0) {
		nf.toast("亲，请写点什么吧，或者添加图片。");
		return;
	}
	var imgFiles = [];
	for (var i = 0; i < length; i++) {
		imgFiles.push(imgsData[i].imgPath);
	}
	if (length > 0) {
		qqHttp.form(imgFiles, "community/message", function(imgDatas) {
			var string4imgs = imgDatas.path;// 返回图片云端路径（后部份）用分号隔开
			var params = {
				"type" : 1,//指定一类【这个类型以后不用了】
				"img" : string4imgs,
				"content" : textBoxContent.text,
				"tagId": tagId
			};
			httpSilence.post(communityMessageUrl, params, function(data) {
				nf.toast("发布成功");
				do_App.closePage({fromPage:'message_add',communityId:data.communityId});
			})
		});
	} else {
		var params = {
			"type" : 1,//指定一类【这个类型以后不用了】
			"img" : "",
			"content" : textBoxContent.text,
			"tagId": tagId
		};
		qqHttp.post(communityMessageUrl, params, function(data) {
			nf.toast("发布成功");
			do_App.closePage({fromPage:'message_add',communityId:data.communityId});
		})
	}
}
//点击背景隐藏输入键盘
root.on("touch",function(){
	do_Page.hideKeyboard();
})